<?php
class loader{

	$wappFiles = array();
	/**
	 *
	 * Gets the #WaterFramework Application URL
	 *
	 */
	function waterURL(){
		define('ABSPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');

		$tempPath1 = explode('/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
		$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
		$tempPath3 = explode('/', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));

		$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';


		for ($i = count($tempPath2); $i < count($tempPath1); $i++)
		array_pop ($tempPath3);

		$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);

		if ($urladdr{strlen($urladdr) - 1}== '/')
		define('WATERURL', $protocol .'://'. $urladdr);
		else
		define('WATERURL', $protocol .'://'. $urladdr . '/');

		unset($tempPath1, $tempPath2, $tempPath3, $urladdr);
	}

	/**
	 *
	 * Converts paths to proper html paths
	 * @param unknown_type $relative_path
	 */
	private function htmlpath($relative_path){
		$realpath=realpath($relative_path);
		$htmlpath=str_replace($_SERVER['DOCUMENT_ROOT'],'',$realpath);
		return $htmlpath;
	}


	/**
	 * Get files recursively
	 */
	private function getFiles(&$rdi,$depth=0) {
		if (!is_object($rdi))
		return;
			
		for ($rdi->rewind();$rdi->valid();$rdi->next()) {

			if ($rdi->isDot())
			continue;

			if ($rdi->isDir() || $rdi->isFile()) {
					
				require_once(htmlpath($rdi->current()));
					
				if ($rdi->hasChildren()){
					$children = $rdi->getChildren();
					getFiles($children,$depth++);
				}
			}
		}
	}

	/**
	 * Loads files
	 */
	function loadFiles($path){
		$this->getFiles(new RecursiveDirectoryIterator($path));
	}

	function loadWaterPlugin($pluginName){
		$dir = APPLICATION_FOLDER.'/'.$pluginName;

	}


	/**
	 * Function to load controller. This function is called by bootstrap.php
	 * to automatically load all controllers
	 */
	function loadController(){
		if(!isset($water_config['application'])) die('Base Application not set!');
		

		$file = APPLICATION_FOLDER.'/'.$water_config['application'].'/controller/'.$water_config['application'].'.php';
		if(!file_exists($file)) die($water_config['application'].'.php not found');
		
		if(!defined($annotations)){
			$annotations = new annotations();
		}
		$controller = $annotations->parse($file);

		if(method_exists($controller, $this->uri['method'])){

			$controller->{$this->uri['method']}($this->uri['var']);
		} else {
			$controller->index();
		}
	}

	/**
	 * Load all controllers
	 */
	function loadControllers($dir){
		$cDir = $dir.'/controller/';
		$this->loadFiles($cDir);
	}

	/**
	 * Function to load all views.
	 */
	function loadViews($dir){
		$vDir = $dir.'/view/';
		$this->loadFiles($vDir);
	}

	/**
	 * Load all models
	 */
	function loadModels($dir){
		$mDir = $dir.'/model/';
		$this->loadFiles($mDir);
	}

}

?>